package javagprssrv.Common;

import javagprssrv.StructClass.structDYB;
import java.util.Vector;
import java.nio.ByteBuffer;
import java.util.Calendar;
import java.util.Date;
import java.io.FileOutputStream;
import java.io.FileInputStream;
import java.io.File;

public class CommonClass
{
    public boolean FileCopy(final java.lang.String srcFileName, final java.lang.String destFileName) {
        final java.io.File srcFile = new java.io.File(srcFileName);
        final java.io.File destFile = new java.io.File(destFileName);
        try {
            final java.io.FileInputStream fis = new java.io.FileInputStream(srcFile);
            final java.io.FileOutputStream fos = new java.io.FileOutputStream(destFile);
            final byte[] buf = new byte[1024];
            int i = 0;
            while ((i = fis.read(buf)) != -1) {
                fos.write(buf, 0, i);
            }
            fis.close();
            fos.close();
            return true;
        }
        catch (java.lang.Exception e) {
            return false;
        }
    }
    
    public boolean ShellExecute(final java.lang.String strCommand) {
        try {
            java.lang.Runtime.getRuntime().exec(strCommand);
            return true;
        }
        catch (java.lang.Exception e) {
            return false;
        }
    }
    
    public java.lang.String String2Hex(final java.lang.String strMain, final java.lang.String strSpace) {
        java.lang.String ret1 = "";
        java.lang.String ret2 = "";
        for (int i = 0; i < strMain.length(); ++i) {
            final int c = strMain.charAt(i);
            java.lang.String sc = "";
            if (c <= 15) {
                sc = "0" + java.lang.Integer.toHexString(c).toUpperCase();
            }
            else {
                sc = java.lang.Integer.toHexString(c).toUpperCase();
            }
            ret1 += sc;
        }
        for (int i = 0; i < ret1.length() / 2; ++i) {
            ret2 = ret2 + ret1.substring(i * 2, i * 2 + 2) + strSpace;
        }
        ret2 = ret2.substring(0, ret2.length() - strSpace.length());
        return ret2;
    }
    
    public java.lang.String _2BCD(final int intValue) {
        java.lang.String ret = "";
        final int h = intValue / 10;
        final int l = intValue - h * 10;
        final int x = h * 16 + l;
        ret = java.lang.String.valueOf((char)x);
        return ret;
    }
    
    public java.lang.String _BCD2STR(final java.lang.String bcd) {
        final java.lang.StringBuilder sb = new java.lang.StringBuilder();
        for (int i = 0; i < bcd.length(); ++i) {
            final char c = bcd.charAt(i);
            final byte u = (byte)(c >> 4 & 0xFF);
            if (u < 10) {
                sb.append(u);
            }
            else {
                sb.append((char)(65 + (u - 10)));
            }
            final byte l = (byte)(c & '\u000f');
            if (l < 10) {
                sb.append(l);
            }
            else {
                sb.append((char)(65 + (l - 10)));
            }
        }
        return sb.toString();
    }
    
    public java.lang.String int2Bin(final int i) {
        java.lang.String ret = "";
        final char chr_Hi = (char)(i / 256);
        final char chr_Lo = (char)(i % 256);
        ret = "" + chr_Lo + chr_Hi;
        return ret;
    }
    
    public java.lang.String doubleDY2A07(double dy) {
        java.lang.String ret = "";
        if (dy >= 1000.0) {
            dy /= 1000.0;
        }
        dy *= 100.0;
        java.lang.String DY = java.lang.String.valueOf(new Double(dy).intValue());
        DY = "00000000" + DY;
        DY = DY.substring(DY.length() - 5);
        final int c1 = java.lang.Integer.valueOf(DY.substring(0, 2));
        final int c2 = java.lang.Integer.valueOf(DY.substring(2, 4));
        final int c3 = java.lang.Integer.valueOf(DY.substring(4, 5));
        ret = "" + (char)(c3 * 16 + 15) + (char)c2 + (char)c1;
        ret = "" + (char)(c3 * 16 + 15) + this._2BCD(java.lang.Integer.valueOf(DY.substring(2, 4))) + this._2BCD(java.lang.Integer.valueOf(DY.substring(0, 2)));
        return ret;
    }
    
    public java.lang.String GetTimeStampString() {
        java.lang.String ret = "";
        final java.util.Date _date = new java.util.Date();
        int year = 0;
        int month = 0;
        int date = 0;
        int hour = 0;
        int min = 0;
        int sec = 0;
        final java.util.Calendar C = java.util.Calendar.getInstance();
        year = C.get(1);
        month = C.get(2) + 1;
        date = C.get(5);
        hour = C.get(11);
        min = C.get(12);
        sec = C.get(13);
        ret = this._2BCD(year - 2000) + this._2BCD(month) + this._2BCD(date) + this._2BCD(hour) + this._2BCD(min) + this._2BCD(sec);
        return ret;
    }
    
    public java.lang.String Hex2String(java.lang.String HexString) {
        java.lang.String ret = "";
        HexString = HexString.trim().toUpperCase();
        for (int i = 0; i < HexString.length(); i += 2, ++i) {
            int v = -1;
            final java.lang.String s = HexString.substring(i, i + 2);
            v = 16 * this.HexChar2Int(s.charAt(0)) + this.HexChar2Int(s.charAt(1));
            ret += (char)v;
        }
        return ret;
    }
    
    public int HexChar2Int(final char HexChar) {
        int ret = -1;
        final int i = HexChar;
        switch (i) {
            case 48: {
                ret = 0;
                break;
            }
            case 49: {
                ret = 1;
                break;
            }
            case 50: {
                ret = 2;
                break;
            }
            case 51: {
                ret = 3;
                break;
            }
            case 52: {
                ret = 4;
                break;
            }
            case 53: {
                ret = 5;
                break;
            }
            case 54: {
                ret = 6;
                break;
            }
            case 55: {
                ret = 7;
                break;
            }
            case 56: {
                ret = 8;
                break;
            }
            case 57: {
                ret = 9;
                break;
            }
            case 65: {
                ret = 10;
                break;
            }
            case 66: {
                ret = 11;
                break;
            }
            case 67: {
                ret = 12;
                break;
            }
            case 68: {
                ret = 13;
                break;
            }
            case 69: {
                ret = 14;
                break;
            }
            case 70: {
                ret = 15;
                break;
            }
        }
        return ret;
    }
    
    public static byte[] String2ByteArray(final java.lang.String Str) {
        final byte[] b = new byte[Str.length()];
        for (int i = 0; i < Str.length(); ++i) {
            if (Str.charAt(i) >= '\u0080') {
                b[i] = (byte)(Str.charAt(i) - 'Ā');
            }
            else {
                b[i] = (byte)Str.charAt(i);
            }
        }
        return b;
    }
    
    public static java.nio.ByteBuffer String2ByteBuffer(final java.lang.String Str) {
        final java.nio.ByteBuffer buffer = java.nio.ByteBuffer.allocate(Str.length());
        try {
            buffer.clear();
            final byte[] bf = buffer.array();
            for (int ii = 0; ii < Str.length(); ++ii) {
                bf[ii] = (byte)Str.charAt(ii);
            }
            buffer.put(bf);
            buffer.flip();
        }
        catch (java.lang.Exception ex) {}
        return buffer;
    }
    
    public static java.lang.String ByteArray2String(final byte[] b, final int length) {
        java.lang.String res = "";
        int k = length;
        if (k > 4096) {
            k = 4096;
        }
        for (int i = 0; i < k; ++i) {
            if (b[i] < 0) {
                res += (char)(256 + b[i]);
            }
            else {
                res += (char)b[i];
            }
        }
        return res;
    }
    
    public static java.lang.String ByteBuffer2String(final java.nio.ByteBuffer buffer) {
        java.lang.String res = "";
        final int k = buffer.remaining();
        final byte[] b = buffer.array();
        try {
            for (int i = 0; i < k; ++i) {
                if (b[i] < 0) {
                    res += (char)(256 + b[i]);
                }
                else {
                    res += (char)b[i];
                }
            }
        }
        catch (java.lang.Exception e) {
            e.printStackTrace();
        }
        return res;
    }
    
    public static java.util.Vector String2DYBVector(java.lang.String DYBString, final java.lang.String SP1, final java.lang.String SP2, final java.lang.String TypeString) {
        final java.util.Vector V = new java.util.Vector();
        while (DYBString.length() > 0) {
            javagprssrv.StructClass.structDYB dyb = new javagprssrv.StructClass.structDYB();
            final int idx1 = DYBString.indexOf(SP1);
            if (idx1 > -1) {
                final java.lang.String str = DYBString.substring(0, idx1);
                final int idx2 = str.indexOf(SP2);
                if (idx2 > -1) {
                    dyb.Name = str.substring(0, idx2);
                    dyb.Value = str.substring(idx2 + SP2.length(), str.length());
                    dyb.Type = 1;
                    if (dyb.Value.substring(0, TypeString.length()).equals(TypeString) && dyb.Value.substring(dyb.Value.length() - TypeString.length(), dyb.Value.length()).equals(TypeString)) {
                        dyb.Type = 0;
                    }
                    V.add(dyb);
                    dyb = null;
                }
                DYBString = DYBString.substring(idx1 + 1, DYBString.length());
            }
            else {
                final java.lang.String str = DYBString;
                final int idx2 = str.indexOf(SP2);
                if (idx2 > -1) {
                    dyb.Name = str.substring(0, idx2);
                    dyb.Value = str.substring(idx2 + SP2.length(), str.length());
                    dyb.Type = 1;
                    if (dyb.Value.substring(0, TypeString.length()).equals(TypeString) && dyb.Value.substring(dyb.Value.length() - TypeString.length(), dyb.Value.length()).equals(TypeString)) {
                        dyb.Type = 0;
                    }
                    V.add(dyb);
                    dyb = null;
                }
                DYBString = "";
            }
        }
        return V;
    }
}
